<?php
class ModelConfigureCloudzone extends Model {
    public function addCloudzone($data) {
        $this->db->query("INSERT INTO `" . DB_PREFIX . "configure_cloudzone` SET type = '" . $this->db->escape($data['type']) . "', sort_order = '" . (int)$data['sort_order'] . "'");

        $cloudzone_id = $this->db->getLastId();

        foreach ($data['cloudzone_description'] as $language_id => $value) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "cloudzone_description SET cloudzone_id = '" . (int)$cloudzone_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "'");
        }

        if (isset($data['cloudzone_value'])) {
            foreach ($data['cloudzone_value'] as $cloudzone_value) {
                $this->db->query("INSERT INTO " . DB_PREFIX . "cloudzone_value SET cloudzone_id = '" . (int)$cloudzone_id . "', image = '" . $this->db->escape(html_entity_decode($cloudzone_value['image'], ENT_QUOTES, 'UTF-8')) . "', sort_order = '" . (int)$cloudzone_value['sort_order'] . "'");

                $cloudzone_value_id = $this->db->getLastId();

                foreach ($cloudzone_value['cloudzone_value_description'] as $language_id => $cloudzone_value_description) {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "cloudzone_value_description SET cloudzone_value_id = '" . (int)$cloudzone_value_id . "', language_id = '" . (int)$language_id . "', cloudzone_id = '" . (int)$cloudzone_id . "', name = '" . $this->db->escape($cloudzone_value_description['name']) . "'");
                }
            }
        }
    }

    public function editCloudzone($cloudzone_id, $data) {
        $this->db->query("UPDATE `" . DB_PREFIX . "configure_cloudzone` SET type = '" . $this->db->escape($data['type']) . "', sort_order = '" . (int)$data['sort_order'] . "' WHERE cloudzone_id = '" . (int)$cloudzone_id . "'");

        $this->db->query("DELETE FROM " . DB_PREFIX . "cloudzone_description WHERE cloudzone_id = '" . (int)$cloudzone_id . "'");

        foreach ($data['cloudzone_description'] as $language_id => $value) {
            $this->db->query("INSERT INTO " . DB_PREFIX . "cloudzone_description SET cloudzone_id = '" . (int)$cloudzone_id . "', language_id = '" . (int)$language_id . "', name = '" . $this->db->escape($value['name']) . "'");
        }

        $this->db->query("DELETE FROM " . DB_PREFIX . "cloudzone_value WHERE cloudzone_id = '" . (int)$cloudzone_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "cloudzone_value_description WHERE cloudzone_id = '" . (int)$cloudzone_id . "'");

        if (isset($data['cloudzone_value'])) {
            foreach ($data['cloudzone_value'] as $cloudzone_value) {
                if ($cloudzone_value['cloudzone_value_id']) {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "cloudzone_value SET cloudzone_value_id = '" . (int)$cloudzone_value['cloudzone_value_id'] . "', cloudzone_id = '" . (int)$cloudzone_id . "', image = '" . $this->db->escape(html_entity_decode($cloudzone_value['image'], ENT_QUOTES, 'UTF-8')) . "', sort_order = '" . (int)$cloudzone_value['sort_order'] . "'");
                } else {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "cloudzone_value SET cloudzone_id = '" . (int)$cloudzone_id . "', image = '" . $this->db->escape(html_entity_decode($cloudzone_value['image'], ENT_QUOTES, 'UTF-8')) . "', sort_order = '" . (int)$cloudzone_value['sort_order'] . "'");
                }

                $cloudzone_value_id = $this->db->getLastId();

                foreach ($cloudzone_value['cloudzone_value_description'] as $language_id => $cloudzone_value_description) {
                    $this->db->query("INSERT INTO " . DB_PREFIX . "cloudzone_value_description SET cloudzone_value_id = '" . (int)$cloudzone_value_id . "', language_id = '" . (int)$language_id . "', cloudzone_id = '" . (int)$cloudzone_id . "', name = '" . $this->db->escape($cloudzone_value_description['name']) . "'");
                }
            }
        }
    }

    public function deleteCloudzone($cloudzone_id) {
        $this->db->query("DELETE FROM `" . DB_PREFIX . "configure_cloudzone` WHERE cloudzone_id = '" . (int)$cloudzone_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "cloudzone_description WHERE cloudzone_id = '" . (int)$cloudzone_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "cloudzone_value WHERE cloudzone_id = '" . (int)$cloudzone_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "cloudzone_value_description WHERE cloudzone_id = '" . (int)$cloudzone_id . "'");
    }

    public function getCloudzone($cloudzone_id) {
        $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "configure_cloudzone` o LEFT JOIN " . DB_PREFIX . "cloudzone_description od ON (o.cloudzone_id = od.cloudzone_id) WHERE o.cloudzone_id = '" . (int)$cloudzone_id . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");

        return $query->row;
    }

    public function getCloudzones($data = array()) {
        $sql = "SELECT * FROM `" . DB_PREFIX . "configure_cloudzone` o LEFT JOIN " . DB_PREFIX . "configure_cloudzone_description od ON (o.cloudzone_id = od.cloudzone_id) WHERE od.language_id = '" . (int)$this->config->get('config_language_id') . "'";

        if (isset($data['filter_name']) && !is_null($data['filter_name'])) {
            $sql .= " AND od.name LIKE '" . $this->db->escape($data['filter_name']) . "%'";
        }

        $sort_data = array(
            'od.name',
            'o.type',
            'o.sort_order'
        );

        if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
            $sql .= " ORDER BY " . $data['sort'];
        } else {
            $sql .= " ORDER BY od.name";
        }

        if (isset($data['order']) && ($data['order'] == 'DESC')) {
            $sql .= " DESC";
        } else {
            $sql .= " ASC";
        }

        if (isset($data['start']) || isset($data['limit'])) {
            if ($data['start'] < 0) {
                $data['start'] = 0;
            }

            if ($data['limit'] < 1) {
                $data['limit'] = 20;
            }

            $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
        }

        $query = $this->db->query($sql);

        return $query->rows;
    }

    public function getCloudzoneDescriptions($cloudzone_id) {
        $cloudzone_data = array();

        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cloudzone_description WHERE cloudzone_id = '" . (int)$cloudzone_id . "'");

        foreach ($query->rows as $result) {
            $cloudzone_data[$result['language_id']] = array('name' => $result['name']);
        }

        return $cloudzone_data;
    }

    public function getCloudzoneValue($cloudzone_value_id) {
        $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cloudzone_value ov LEFT JOIN " . DB_PREFIX . "cloudzone_value_description ovd ON (ov.cloudzone_value_id = ovd.cloudzone_value_id) WHERE ov.cloudzone_value_id = '" . (int)$cloudzone_value_id . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

        return $query->row;
    }

    public function getCloudzoneValues($cloudzone_id) {
        $cloudzone_value_data = array();

        $cloudzone_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cloudzone_value ov LEFT JOIN " . DB_PREFIX . "cloudzone_value_description ovd ON (ov.cloudzone_value_id = ovd.cloudzone_value_id) WHERE ov.cloudzone_id = '" . (int)$cloudzone_id . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "' ORDER BY ov.sort_order ASC");

        foreach ($cloudzone_value_query->rows as $cloudzone_value) {
            $cloudzone_value_data[] = array(
                'cloudzone_value_id' => $cloudzone_value['cloudzone_value_id'],
                'name'            => $cloudzone_value['name'],
                'image'           => $cloudzone_value['image'],
                'sort_order'      => $cloudzone_value['sort_order']
            );
        }

        return $cloudzone_value_data;
    }

    public function getCloudzoneValueDescriptions($cloudzone_id) {
        $cloudzone_value_data = array();

        $cloudzone_value_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cloudzone_value WHERE cloudzone_id = '" . (int)$cloudzone_id . "'");

        foreach ($cloudzone_value_query->rows as $cloudzone_value) {
            $cloudzone_value_description_data = array();

            $cloudzone_value_description_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "cloudzone_value_description WHERE cloudzone_value_id = '" . (int)$cloudzone_value['cloudzone_value_id'] . "'");

            foreach ($cloudzone_value_description_query->rows as $cloudzone_value_description) {
                $cloudzone_value_description_data[$cloudzone_value_description['language_id']] = array('name' => $cloudzone_value_description['name']);
            }

            $cloudzone_value_data[] = array(
                'cloudzone_value_id'          => $cloudzone_value['cloudzone_value_id'],
                'cloudzone_value_description' => $cloudzone_value_description_data,
                'image'                    => $cloudzone_value['image'],
                'sort_order'               => $cloudzone_value['sort_order']
            );
        }

        return $cloudzone_value_data;
    }

    public function getTotalCloudzones() {
        $query = $this->db->query("SELECT COUNT(*) AS total FROM `" . DB_PREFIX . "configure_cloudzone`");

        return $query->row['total'];
    }
}
?>